package com.base.vistter.iframe.service;

import com.base.vistter.iframe.entity.InverterDevicesPowerMonth;
import com.base.vistter.iframe.entity.InverterDevicesPowerYear;
import com.base.vistter.iframe.mapper.InverterDevicesPowerYearMapper;
import com.base.vistter.iframe.model.DevicesMonthPowerRequest;
import com.base.vistter.iframe.model.DevicesYearPowerRequest;
import com.base.vistter.iframe.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
@Slf4j
public class InverterDevicesPowerYearService extends BaseService<InverterDevicesPowerYearMapper, InverterDevicesPowerYear> {

    @Transactional(readOnly = true)
    public List<InverterDevicesPowerYear> statisticsDevices(DevicesYearPowerRequest devicesYearPowerRequest) {
        String column = Arrays.stream(devicesYearPowerRequest.getColumns()).map(item -> "`" + item + "`").collect(Collectors.joining(","));
        Date startTime = DateUtils.getYearStartTime(devicesYearPowerRequest.getTime());
        Date endTime = DateUtils.getYearEndTime(devicesYearPowerRequest.getTime());
        List<InverterDevicesPowerYear> list = this.baseMapper.statisticsDevicesYear(column, devicesYearPowerRequest.getDevicesId(), startTime, endTime);
        //如果是本月统计，则添加今日统计数据
//        if (DateUtils.isCurrentYear(devicesYearPowerRequest.getTime())) {
//            list.add(this.baseMapper.getTodayPower(column, devicesYearPowerRequest.getDevicesId(), DateUtils.getTodayStartTime(), DateUtils.getTodayEndTime()));
//        }
        return list;
    }

    @Transactional(readOnly = true)
    List<InverterDevicesPowerYear> statisticsYear(Integer stationId, Integer time) {
        return this.baseMapper.statisticsYear(stationId, time);
    }

}
